package com.ymatou.iapi.liveinfo.testcase;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveinfo.parameter.ListInProgressActivitiesBySellerIdsBean;
import com.ymatou.iapi.liveinfo.service.ListInProgressActivitiesBySellerIdsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.database.mongo.jproduct.LivesWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*根据卖家id列表获取正在进行中直播列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ListInProgressActivitiesBySellerIds{
	//创建正在进行的直播的用户
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	//创建正在进行的直播的用户
	private static int sellerId4 = Integer.parseInt(EnvSetup.getData("productsellerid6"));
	private static String sellerName4 = EnvSetup.getData("productsellername6");
	//创建已经结束的直播的用户
	private static int sellerId2 = Integer.parseInt(EnvSetup.getData("productsellerid5"));
	private static String sellerName2 = EnvSetup.getData("productsellername5");
	//卖家没有创建直播的用户
	private static int sellerId3 = Integer.parseInt(EnvSetup.getData("productsellerid7"));
	private static String sellerName3 = EnvSetup.getData("productsellername7");
	
	private static ListInProgressActivitiesBySellerIdsBean bean;
	private static ListInProgressActivitiesBySellerIdsCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据卖家id列表获取正在进行中直播列表");
	}
	@Before
	public void caseUp() {
		bean=new ListInProgressActivitiesBySellerIdsBean();
		call=new ListInProgressActivitiesBySellerIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListInProgressActivitiesBySellerIds_001() {
		Logger.start(true,"根据卖家id列表获取正在进行中直播列表-当前卖家id列表都有正在进行的直播,且卖家id列表长度为1-返回卖家id列表当前正在进行的直播");
		try {
			int[] sellIds=new int[1];
		    JSONObject info = LiveCallService.createLiveInProgress(sellerName,sellerId);
		    
		    String ActivityId= info.getJSONObject("Data").getString("ActivityId");
		    Logger.debug(ActivityId);
			LivesWapper wapper = new LivesWapper();
			String cover="http://m.ymatou.com/"+TestDataManager.getRandomUUID().toString();
			wapper.updateCover(ActivityId.toString(), cover);
		    sellIds[0]=sellerId;
			bean.setSellerIds(sellIds);
			call.setData(bean);
			call.callService();	
			GetVerify(sellIds);				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ListInProgressActivitiesBySellerIds_002() {
		Logger.start(true,"根据卖家id列表获取正在进行中直播列表-当前卖家id列表都有正在进行的直播,且卖家id列表长度为2-返回卖家id列表当前正在进行的直播");
		try {
			int[] sellIds=new int[2];
		    LiveCallService.createLiveInProgress(sellerName,sellerId);
		    LiveCallService.createLiveInProgress(sellerName4,sellerId4);
		    sellIds[0]=sellerId;
		    sellIds[1]=sellerId4;
			bean.setSellerIds(sellIds);
			call.setData(bean);
			call.callService();	
			GetVerify(sellIds);	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ListInProgressActivitiesBySellerIds_003() {
		Logger.start(true,"根据卖家id列表获取正在进行中直播列表-当前卖家id列表有部分正在进行的直播-返回卖家id列表当前部分正在进行的直播");
		try {
			int[] sellIds=new int[2];
		    LiveCallService.createLiveInProgress(sellerName,sellerId);
		    int lid=LiveCallService.createLiveInProgress(sellerName4,sellerId4).getJSONObject("Data").getInt("ActivityId");
		    LiveCallService.closeLive(lid, sellerId4);
		    sellIds[0]=sellerId;
		    sellIds[1]=sellerId4;
			bean.setSellerIds(sellIds);
			call.setData(bean);
			call.callService();	
			GetVerify(sellIds);			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ListInProgressActivitiesBySellerIds_004() {
		Logger.start(false,"根据卖家id列表获取正在进行中直播列表-当前卖家id列表没有正在进行的直播-返回空");
		try {
			int[] sellIds=new int[1];
		    sellIds[0]=sellerId3;
			bean.setSellerIds(sellIds);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals(200, call.getCode(), "判断code是否为200");
			Logger.verifyEquals("操作成功",call.getMsg(), "判断Msg返回操作成功");
			Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
			Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
			Logger.verifyEquals(0, call.getData().getInt("ActivityCount"), "验证直播返回数量ActivityCount为0");
			Logger.verifyEquals("[]",call.GetActivities().toString(), "判断服务器时间是否为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void GetVerify(int[] sellIds) throws Exception
	{
		LivesWapper livesWapper=new LivesWapper();
		if(sellIds.length>0){
			Logger.verifyEquals(200, call.getCode(), "判断code是否为200");
			Logger.verifyEquals("操作成功",call.getMsg(), "判断Msg返回操作成功");
			Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
			Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
			
			ArrayList<Map> lst=livesWapper.getLivesListBySellerIds(sellIds);
			Logger.verifyEquals(lst.size(), call.getData().getInt("ActivityCount"), "验证直播返回数量ActivityCount是否与mongodb一致");
		for(int i=0;i<lst.size();i++){	
			Logger.verifyEquals(lst.get(i).get("title").toString(), call.GetActivities().getJSONObject(i).getString("Title"), "验证直播title是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("vcover").toString(), call.GetActivities().getJSONObject(i).getString("VideoCover"), "验证直播VideoCover是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("vurl").toString(), call.GetActivities().getJSONObject(i).getString("VideoUrl"), "验证直播VideoUrl是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("action").toString().toString(), call.GetActivities().getJSONObject(i).getString("Action").toString(), "验证直播Action是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("lid").toString(), call.GetActivities().getJSONObject(i).getString("ActivityId").toString(), "验证直播ActivityId是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("name").toString(), call.GetActivities().getJSONObject(i).getString("ActivityName"), "验证直播ActivityName是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("pic").toString(), call.GetActivities().getJSONObject(i).getString("ActivityPicture"), "验证直播ActivityPicture是否与mongodb一致");
			
			Logger.verifyEquals(lst.get(i).get("cover")==null?"null":lst.get(i).get("cover").toString() ,call.GetActivities().getJSONObject(i).getString("LiveCover"), "验证直播ActivityPicture是否与mongodb一致");
			
//			String add=lst.get(i).get("add").toString().replace("\"$date\":","").replace("{", "").replace("}", "").replace("\"", "").toString();
//			add=add.substring(0,add.length()-5).replace("T", " ");
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get(i).get("add")).toString(), call.GetActivities().getJSONObject(i).getString("AddTime").toString(), "验证直播AddTime是否与mongodb一致");
			
			Logger.verifyEquals(lst.get(i).get("country").toString(), call.GetActivities().getJSONObject(i).getString("Country"), "验证直播Country是否与mongodb一致");
			
//			String end=lst.get(i).get("end").toString().replace("\"$date\":","").replace("{", "").replace("}", "").replace("\"", "").toString();
//			end=end.substring(0,end.length()-5).replace("T", " ");
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get(i).get("end")).toString(), call.GetActivities().getJSONObject(i).getString("EndTime"), "验证直播EndTime是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("sid").toString(), call.GetActivities().getJSONObject(i).getString("SellerId").toString(), "验证直播SellerId是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("addr").toString(), call.GetActivities().getJSONObject(i).getString("ShopAddress"), "验证直播ShopAddress是否与mongodb一致");
			
//			String start=lst.get(i).get("start").toString().replace("\"$date\":","").replace("{", "").replace("}", "").replace("\"", "").toString();
//			start=start.substring(0,start.length()-5).replace("T", " ");
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get(i).get("start")).toString(), call.GetActivities().getJSONObject(i).getString("StartTime"), "验证直播StartTime是否与mongodb一致");
			Logger.verifyEquals(lst.get(i).get("content").toString(), call.GetActivities().getJSONObject(i).getString("ActivityInfo"), "验证直播ActivityInfo是否与mongodb一致");
			
			String start=(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get(i).get("start"));
			String end=(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get(i).get("end"));
			Logger.verifyEquals(GetActivityStatusText(lst,i,start,end), call.GetActivities().getJSONObject(i).getString("ActivityStatusText"), "验证直播ActivityStatusText是否与mongodb一致");
		}
	  }
	}
	private String GetActivityStatusText(ArrayList<Map> lst,int num,String start1,String end1) throws ParseException, JSONException
	{
		String text="";
		Date currentTime = new Date();

		if((int)lst.get(num).get("action")==1 )
		{
			Calendar calendar  =  new   GregorianCalendar(); 
			calendar.setTime(currentTime); 
			calendar.add(calendar.HOUR,1);
			Date newCurrentTime=calendar.getTime();
			
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			Date start=format.parse(start1);
			Date end=format.parse(end1);
			
			if((boolean)lst.get(num).get("confirm")==true && ((currentTime.after(start)|| currentTime==start)&& (end.after(currentTime)|| currentTime==end)))
				text="直播中";
			else if((boolean)lst.get(num).get("confirm")==true && start.after(currentTime) && end.after(start))
				text="即将开始";
			else if((boolean)lst.get(num).get("confirm")==false && end.after(newCurrentTime))
				text="未生效";
			else
				text="已结束";
		}
		return text;
	}
}